<template>
    <div>
      <Form  :model="formCustom" :label-width="50">
        <p>头像</p>
        <div style="padding-left: 55px;margin: 15px">

          <el-upload
              class="avatar-uploader"
              action="string"
              :show-file-list="false"
              :http-request="uploadImg"
              :before-upload="beforeAvatarUpload">
            <img v-if="formCustom.img" :src="formCustom.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <FormItem label="姓名" style="width: 360px">
          <Input v-model="formCustom.name"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="handleSubmit">提交</Button>
        </FormItem>
      </Form>
    </div>
</template>

<script>
export default {
name: "personal",
  data () {
    return{

    }
  },
  methods: {
    handleSubmit () {
      this.http.post("uploadImg",this.formCustom).then(res=>{
          if (res.data){
            this.$Message.success('提交成功')
          }else {
            this.$Message.error('失败')
          }
        })
    },
    uploadImg(param) {
     const formData=new FormData();
     formData.append("file",param.file)
      this.http.post("upload",formData).then(res=>{
        if (res.data){
          this.formCustom.img=res.data
           console.log(this.formCustom.id)
        }else {
          this.$Message.error('失败')
        }
      })
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  computed:{
    formCustom(){
      return this.$store.state.user
    }
  },
  created() {

  }
}
</script>

<style scoped>
.avatar-uploader  {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  width: 178px;
  position: relative;
  overflow: hidden;
}
.avatar-uploader :hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
